<?php
/**
 * 
 * @author tangbl
 * 
 */
class ProductAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
		
	/**
	 * 列表
	 */
	public function index() {
		
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Product");
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->field("description,content,",true)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('cfg_list_itf_btn_modify');
			$del       = L('cfg_list_itf_btn_delete');
			foreach($list AS $row){
				$url = '';
				if($row['type']==1){
					//设为团购
					$url .= '<a href="#" class="btn green tuanOn"   data-id="'.$row['id'].'"><i class="icon-tumblr-sign"></i></a> ';
					//设为秒杀
					$url .= '<a href="#" class="btn green miaoOn"   data-id="'.$row['id'].'"><i class="icon-skype"></i></a>';
				}elseif($row['type']==2){
					//取消团购
					$url .= '<a href="#" class="btn red tuanOff"  data-id="'.$row['id'].'"><i class="icon-tumblr-sign"></i></a> ';
					//秒杀不可用
					$url .= '<a href="#" class="btn" ><i class="icon-skype"></i></a>';
				}elseif($row['type']==3){
					//团购不可用
					$url .= '<a href="#" class="btn" ><i class="icon-tumblr-sign"></i></a>';
					//取消秒杀
					$url .= '<a href="#" class="btn red miaoOff"  data-id="'.$row['id'].'"><i class="icon-skype"></i></a>';
				}
				
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'      => $row['id'],
						'cateid'  => get_cate_name($row['cateid'],$row['lang']),
						'lang'    => $row['lang'],
						'title'   => $row['title'],
						'mprice'  => $row['mprice'],
						'sort'    => $row['sort'],
						'status'  => getProductStatus($row['status']),
						'deal'    => '
					 <a href="#" class="btn green create_url"  data-id="'.$row['id'].'"><i class="icon-link"></i></a>
					  <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
				     <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
					 .$url
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			
			$module = MODULE_NAME;
			$lang   = getLang();
			$cates  = F("cate_".$lang.$module);
			$option = get_cate_select($cates,0,0);
			$this->assign("cates",$option);

			$this->display();
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		if(IS_POST){
		
			$param = array(
				"upload" => array("single"=>array("single"),"atlas"=>array("atlas")),
				"reply"  => true,
				"cache"  => true,
				"after"  => "Product_after_deal"
			);
			parent::do_add("Product",$param);
			
		} else {
			$product =  D("Product");
			$lanset  = getLang();
			$lang    = $this->_param("lang","",$lanset);
			$this->assign("lang",$lang);
			
			$cateid  = $this->_param('cateid','intval',0);
			$this->assign("cateid",$cateid);
			
			load("@.module");
			$module = getSelectCacheKey("Module_fu",array("module_name","title"),true,$lanset);	
			$this->assign("module",$module);
			
			$brands = getSelectCacheKey("Brands",array("id","title"),false,$lanset);
			$this->assign("brands",$brands);
			
			/* $kind   = getVirtualModuleOne("kind","linkage","152","");
			$this->assign("kind",$kind);
			
			$origin = getVirtualModuleOne("origin","linkage","140","");
			$this->assign("origin",$origin); */
			
			$module = MODULE_NAME;
			$cates  = F("cate_".$lanset.$module);
			$option = get_cate_select($cates,0,0);
			$this->assign("cates",$option);
			
			$module = "Article";
			$cates  = F("cate_".$lanset.$module);
			$option = get_cate_select($cates,0,0);
			$this->assign("article",$option);
			
			$brands = F("Brands");
			$option = get_cate_select($brands,0,0);
			$this->assign("brands",$option);
			
			$package= getVirtualModuleOne("package","linkage","135","");
			$this->assign("package",$package);
			
			$this->display();
		}
	}
	
	/**
	 * 关联商品
	 */
	public function relation(){
		
		if(IS_AJAX){
			
			$brandid = $this->_get("brand" ,"intval",0);
			$cateid  = $this->_get("cateid","intval",0);
			if($brandid){
				$m['brandid'] = $this->_get("brand" ,"intval",0);
			}
			if($cateid){
				$m['cateid']  = $this->_get("cateid","intval",0);	
			}
			$m['isdel']= 2;
			$m['lang'] = getLang();
			$m['type'] = 1;
			$model = D("Product");
			$list  = $model->where($m)->field("id,title,ctitle,sprice")->select();
			
			$this->assign("list",$list);
			$content = $this->fetch();
			$this->ajaxReturn($content);
		}else{
			$this->display();
		}
	}
	
	/**
	 * 关联文章
	 */
	public function article(){
	
		if(IS_AJAX){
			
			$cateid  = $this->_get("cateid","intval",0);
			if($cateid){
				$m['cateid']  = $this->_get("cateid","intval",0);
			}
			$m['lang']  = getLang();
			$m['isdel'] = 2;
			$model = D("Article");
			$list  = $model->where($m)->field("id,title")->select();
				
			$this->assign("list",$list);
			$content = $this->fetch();
			$this->ajaxReturn($content);
		}else{
			$this->display();
		}
	}
	
	/**
	 * 逻辑删除
	 */
	public function del(){
		
		$param = array(
			"reply"  => true,
			"cache"  => true,
		);
		parent::do_del("Product",false,$param);
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		$id      = $this->_param('id','intval',0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		if(IS_POST) {
			$param = array(
				"upload" => array("single"=>array("single"),"atlas"=>array("atlas")),
				"reply"  => true,
				"cache"  => true,
				"after"  => "Product_after_deal"
			);
			parent::do_edit("Product",$param);
		}else{
			
			$product = D("Product");
			$data    = $product->find($id);
			$this->assign("product",$data);
			
			$lang  = create_option_key(C("LANG_ARRAY"),$data['lang']);
			$this->assign("lang",$lang);
			
			$single = find_thumb($data['solevar'],'single');
			$this->assign("single",$single);
			
			$atlas  = find_album($data['solevar'],'atlas');
			$this->assign("atlas",$atlas);
			
			load("@.module");
			$module = getSelectCacheKey("Module_fu",array("module_name","title"),true,$data['lang'],$data['module']);
			$this->assign("module",$module);
				
			$brands = getSelectCacheKey("Brands",array("id","title"),false,$data['lang'],$data['brandid']);
			$this->assign("brands",$brands);
				
			/* $kind   = getVirtualModuleOne("kind","linkage","152",$data['kind']);
			$this->assign("kind",$kind);
				
			$origin = getVirtualModuleOne("origin","linkage","140",$data['origin']);
			$this->assign("origin",$origin); */
			
			$module = MODULE_NAME;
			$cates  = F("cate_".$data['lang'].$module);
			$option = get_cate_select($cates,0,0);
			$this->assign("cates",$option);
			
			$module = "Article";
			$cates  = F("cate_".$lanset.$module);
			$option = get_cate_select($cates,0,0);
			$this->assign("article",$option);
				
			$brands = F("Brands");
			$option = get_cate_select($brands,0,0);
			$this->assign("brands",$option);
			
			$module = D("ProductRelationView");
			$tolist = $module->where(array("pid"=>$id))->field("tid,id,title,ctitle,rprice")->select();
			$this->assign("tolist",$tolist);
			
			$module = D("ProductArticleView");
			$tolist = $module->where(array("pid"=>$id))->field("tid,id,title")->select();
			$this->assign("toarticle",$tolist);
				
			$package= getVirtualModuleOne("package","linkage","135",$data['package']);
			$this->assign("package",$package);
			
			$this->display();
		}
	}
	
	/**
	 * 设置商品为团购和秒杀
	 */
	public function activity(){
		
		$id     = $this->_param("id");
		$model  = D("Product");
		$row    = $model->find($id);
		$status = $this->_param("status");
		$type   = $this->_param("type");
		$table  = $this->_param("table");
		if(!$row['id']) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$m = M("$table");
		$map['pid'] = $row['id'] ;
		$one = $m->where($map)->find();
		
		if(IS_AJAX){
			
			$con['id']    = $row['id'] ;
			$con["$type"] = $status ;
			$model->save($con);
				
			$model->where("id=".$row['id'])->setInc("buynum",$one['buynum']);
			$this->success(L('pro_form_tipmsg_activity'));

		}else{//设置为团购和秒杀 
			
			$aid = $one['id'];
			$data['mprice']  = $row['sprice'];
			$data['buynum']  = 0;
			if($one['id']){
				$m->where($map)->save($data);
			}else{
				$data['pid']     = $row['id'];
				$data['solevar'] = create_id();
				$data['addtime'] = time();
				$data['addid']   = session(C('USER_AUTH_KEY'));
				$aid = $m->add($data);
			}
				
			$map['id']    = $row['id'] ;
			$map["$type"] = $status ;
			$model->save($map);
			
			//todo
			//订单详情表需要设置一个字段标志(shoped)上次设置的团购和秒杀的状态
				
			$this->assign("jumpUrl",U($table.'/edit',"id=".$aid));
			$this->success(L('pro_form_tipmsg_activity'));
		}
	}
	
}
?>